<?php /* УТФ-8:fix */

class SPIcontroller__singlepage__manage extends SPIcontroller__singlepage
{
    public function gen_menu()
    {
        $menuitems = array();

        $in_topic = SPI::I('var')->get('topic');
        $in_tmode = SPI::I('var')->get('tmode');
		foreach( array( 'about' => 'О компании', 'contacts' => "Контакты" ) as $key => $value){
			$menuitems['MAIN'][] = array(
				'access'        => array( SPI_IAM_PERM_ADMIN ),
				'link'          => $this->pack_link($key),
				'icon'          => 'singlepage__manage_'.$key.'.gif',
				'name'          => $value,
				'is_active'     => $in_topic == $this->CONTROLLER_ID && $in_tmode == $key
			);
		}

        return $menuitems;
    }

    public function pack_link( $mode = null, $vars = null )
    {
		return SPIc::I('manage')->link->pack( $this->CONTROLLER_ID, $mode, $vars );
    }

    public function exec( & $content )
    {
        if( SPI::I('Iam')->is_permit( SPI_IAM_PERM_ADMIN ) === true )
        {
            # access allow
        }
        else return false;

        $data = array();

        $data['topic']  = SPI::I('var')->get('topic');
        $data['tmode']  = SPI::I('var')->get('tmode');
        $data['action'] = SPI::I('var')->get('action');

        if( ! $data['tmode'] )
        {
            return false;
        }

        switch( $data['action'] )
        {
            default:
            {
                $data['action'] = 'view';
            }
            case 'view':
            {
                if( $this->exec_view( $content, $data ) === false )
                {
                    return false;
                }
                break;
            }

            case 'save':
            {
                $this->exec_savedata( $content, $data ); # => die by json-data response
            }
            case 'form':
            {
                if( $this->exec_form( $content, $data ) === false )
                {
                    return false;
                }
                break;
            }
        }

        $content['data'] = $data;

        return true;
    }

    private function exec_view( & $content, & $data )
    {
        if( false === ( $mSinglepage = SPIm::I('singlepage') ) )
        {
            return false;
        }

        $data['title']    = '';
        $data['template'] = 'singlepage/view.tpl';

        $item_fields = $mSinglepage->manage->get_by_tid( $data['tmode'] );

        if( $item_fields !== false )
        {
            $data['title'] .= ' "'.$item_fields['name'].'"';
        }

        $content['item_fields'] = $item_fields;

        // --------------------------------------------------------------------

        $data['link_modify'] = $this->pack_link( $data['tmode'], array( 'action'=>'form' ) );

        // --------------------------------------------------------------------
        // Make notifications by 'action_complite' value

        switch( SPI::I('var')->get('action_complite') )
        {
            case 'save': SPIc::I('manage')->notifier->set_message( $content, true, SPI::I('lang')->notifier->msg_modify_complite ); break;
        }

        // --------------------------------------------------------------------

        return true;
    }

    private function exec_form( & $content, & $data )
    {
        if( false === ( $mSinglepage = SPIm::I('singlepage') ) )
        {
            return false;
        }

        $data['title']    = SPI::I('lang')->singlepage->title;
        $data['subtitle'] = SPI::I('lang')->manage->viewmode_form_update;
        $data['template'] = 'singlepage/form.tpl';

        $item_fields = $mSinglepage->manage->get_by_tid( $data['tmode'] );

        if( $item_fields !== false )
        {
            $data['title'] .= ' "'.$item_fields['name'].'"';
        }

        $ext_editor = SPIc::I('widget')->spaw2->open();
        $ext_editor->addpage( 'description_ru_ExtEditor', $item_fields['description_ru'] );
        $item_fields['description_ru_ExtEditor'] = $ext_editor->render();
		
        $ext_editor = SPIc::I('widget')->spaw2->open();
		$ext_editor->addpage( 'description_ua_ExtEditor', $item_fields['description_ua'] );
        $item_fields['description_ua_ExtEditor'] = $ext_editor->render();
		
        $ext_editor = SPIc::I('widget')->spaw2->open();
		$ext_editor->addpage( 'description_en_ExtEditor', $item_fields['description_en'] );
        $item_fields['description_en_ExtEditor'] = $ext_editor->render();

        $content['item_fields'] = $item_fields;

        // --------------------------------------------------------------------

        $data['formvars'] = array();
        $data['formvars']['action'] = 'save';
        $data['formvars']['tmode'] = $data['tmode'];

        $data['link_form_submit'] = $this->pack_link();

        // --------------------------------------------------------------------

        return true;
    }

    private function exec_savedata( & $content, & $data )
    {
        if( false === ( $mSinglepage = SPIm::I('singlepage') ) )
        {
            return false;
        }

        if( in_array( $data['action'], array( 'save' ) ) === false )
        {
            trigger_error( 'Undefined action ('.$data['action'].')', E_USER_ERROR );
        }

        // --------------------------------------------------------------------

        $in_values = SPI::I('var')->get('field');

        // --------------------------------------------------------------------

        $validator = SPIc::I('validator')->open();

		$validator->add_rule( 'name_ru',           'string[1:255]' );
        $validator->add_rule( 'name_ua',           'string[1:255]' );
        $validator->add_rule( 'name_en',           'string[1:255]' );

        $validator->add_rule( 'description_ru',    'string:ExtEditorClean' );
        $validator->add_rule( 'description_ua',    'string:ExtEditorClean' );
        $validator->add_rule( 'description_en',    'string:ExtEditorClean' );

		
        $validator->add_rule( 'meta_title_ru',          'string' );
        $validator->add_rule( 'meta_title_ua',          'string' );
        $validator->add_rule( 'meta_title_en',          'string' );
		
        $validator->add_rule( 'meta_description_ru',          'string' );
        $validator->add_rule( 'meta_description_ua',          'string' );
        $validator->add_rule( 'meta_description_en',          'string' );
		
        $validator->add_rule( 'meta_keywords_ru',          'string' );
        $validator->add_rule( 'meta_keywords_ua',          'string' );
        $validator->add_rule( 'meta_keywords_en',          'string' );

        $validator->setnull_by_fixvalue( 'meta_title_ru',          '' );
        $validator->setnull_by_fixvalue( 'meta_title_ua',          '' );
        $validator->setnull_by_fixvalue( 'meta_title_en',          '' );
        $validator->setnull_by_fixvalue( 'meta_description_ru',    '' );
        $validator->setnull_by_fixvalue( 'meta_description_ua',    '' );
        $validator->setnull_by_fixvalue( 'meta_description_en',    '' );
        $validator->setnull_by_fixvalue( 'meta_keywords_ru',       '' );
        $validator->setnull_by_fixvalue( 'meta_keywords_ua',       '' );
        $validator->setnull_by_fixvalue( 'meta_keywords_en',       '' );

        list( $status, $validator_data ) = $validator->exec( $in_values );

        if( $status === false )
        {
            $response = array();
            $response['status'] = 'error';
			
			$error_name = '';
			if( isset( $validator_data['name_ru'] ) )
			{
				$error_name .= "[RU] ".$validator_data['name_ru']."<br>";
				unset($validator_data['name_ru']);
			}
			if(	isset( $validator_data['name_ua'] ) )
			{
				$error_name .= "[UA] ".$validator_data['name_ua']."<br>";
				unset($validator_data['name_ua']);
			}
			if(	isset( $validator_data['name_en'] ) )
			{
				$error_name .= "[EN] ".$validator_data['name_en']."<br>";
				unset($validator_data['name_en']);
			}
			
			if( $error_name )
				$validator_data['name'] = $error_name;
				
            $response['error_messages'] = $validator_data; // as errors
            SPIv::I('json')->render($response);
            die();
        }

        // --------------------------------------------------------------------
        // Pre save actions

        $in_values = $validator_data; // as parsed values

        // --------------------------------------------------------------------
        // Do save

        $status = $mSinglepage->manage->save( $data['tmode'] , $in_values );

        if( $status !== false )
        {
            $status = true;
        }

        // --------------------------------------------------------------------
        // Post save actions

        # ...

        // --------------------------------------------------------------------

        $response = array();
        $response['status'] = 'complite';
        $response['link_redirect'] = $this->pack_link( $data['tmode'], array( 'action'=>'view', 'action_complite' => 'save' ) );
        SPIv::I('json')->render($response);
        die();
    }
}
